package com.lb.shopguide.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.BeepManager;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.event.guide.SearchBarcodeCheckoutEvent;
import com.lb.shopguide.event.guide.SearchBarcodeSelectgoodsEvent;
import com.lb.shopguide.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by kang on 2017/5/24.
 */

public class ScanSmallActivity extends BaseActivity implements DecoratedBarcodeView.TorchListener, View.OnClickListener {

    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private Button switchFlashlightButton;
    private EditText etBarcode;
    private ImageView ivSearch;
    private RelativeLayout layoutSearch;

    private BeepManager beepManager;

    private boolean isTorchOn = false;
    private int mode;
    private int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_scanner);
        bindData();

        barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
        etBarcode = findViewById(R.id.et_barcode);
        ivSearch = findViewById(R.id.btn_search);
        layoutSearch = findViewById(R.id.layout_search);
        barcodeScannerView.setTorchListener(this);

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode(barcodeCallback);

        switchFlashlightButton = findViewById(R.id.switch_flashlight);
        switchFlashlightButton.setOnClickListener(this);
        if (mode == AppConstant.SCAN_RETURN_BARCODE) {
            layoutSearch.setVisibility(View.GONE);
        } else if (mode == AppConstant.SCAN_RETURN_GOODS) {
            layoutSearch.setVisibility(View.VISIBLE);
        }


        beepManager = new BeepManager(this);
        initListener();
    }

    private void bindData() {
        if (getIntent().hasExtra(AppConstant.SCAN_MODE)) {
            mode = getIntent().getIntExtra(AppConstant.SCAN_MODE, 0);
        }
        if (getIntent().hasExtra(AppConstant.SCAN_FROM)) {
            from = getIntent().getIntExtra(AppConstant.SCAN_FROM, 0);
        }
    }


    private void initListener() {
        ivSearch.setOnClickListener(this);
        etBarcode.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                startSearch();
                return true;
            }
        });
    }

    private BarcodeCallback barcodeCallback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            beepManager.playBeepSound();
            if (mode == AppConstant.SCAN_RETURN_BARCODE) {
                Intent intent = new Intent();
                intent.putExtra(AppConstant.BARCODE, result.getText());
                setResult(RESULT_OK, intent);
                finish();
            } else if (mode == AppConstant.SCAN_RETURN_GOODS) {
                etBarcode.setText(result.getText());
                pushEvent(result.getText());
                finish();
            }
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {

        }
    };

    private void startSearch() {
        String barcode = etBarcode.getText().toString();
        if (!TextUtils.isEmpty(barcode)) {
            pushEvent(barcode);
            finish();
        } else {
            ToastUtils.showShort("请输入条码再搜索");
        }
    }

    private void pushEvent(String barcode) {
        if (from == AppConstant.SCAN_FROM_CHECKOUT) {
            EventBus.getDefault().post(new SearchBarcodeCheckoutEvent(barcode));
        } else if (from == AppConstant.SCAN_FROM_SELECT_GOODS) {
            EventBus.getDefault().post(new SearchBarcodeSelectgoodsEvent(barcode));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }


    @Override
    public void onTorchOn() {
        isTorchOn = true;
    }

    @Override
    public void onTorchOff() {
        isTorchOn = false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.switch_flashlight:
                if (isTorchOn) {
                    barcodeScannerView.setTorchOff();
                } else {
                    barcodeScannerView.setTorchOn();
                }
                break;
            case R.id.btn_search:
                startSearch();
                break;
        }
    }
}
